package cn.innosmart.aq.view.activity.base;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import com.zhy.autolayout.AutoLayoutActivity;

import cn.innosmart.aq.R;
import cn.innosmart.aq.application.AQApplication;
import cn.innosmart.aq.customize.CustomsizeDialog;
import cn.innosmart.aq.customize.DetailLoadingDialog;

/**
 * Created by David on 2015/6/12.
 */
public class BaseActivity extends AutoLayoutActivity {
    public final int EXIT = 0;
    //显示短通知
    private final int SHOWTOAST = 1;
    //显示长通知
    private final int SHOWLONGTOAST = 2;
    public boolean isResume = false;
    private Toast mToast;
    private Handler mHander = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            String message = (String) msg.obj;
            switch (msg.what) {
                case SHOWTOAST:
                    mToast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT);
                    mToast.show();
                    break;
                case SHOWLONGTOAST:
                    mToast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG);
                    mToast.show();
                    break;
            }
        }
    };
    private DetailLoadingDialog loadingDialog;
    private CustomsizeDialog circleDialog;
    private String Tag = "BaseActivity";
    private Context context;
    private CustomsizeDialog connectErrorDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onResume() {
        logW(getClassName() + "onResume!");
        isResume = true;
        super.onResume();
    }

    @Override
    protected void onStop() {
        logW(getClassName() + "onStop!");
        isResume = false;
        super.onStop();
    }

    @Override
    protected void onStart() {
        logW(getClassName() + "onStart!");
        super.onStart();
    }

    @Override
    protected void onDestroy() {
        logW(getClassName() + "onDestory!");
        isResume = false;
        super.onDestroy();
    }

    /**
     * 显示正常通知
     * @param message
     */
    public void showToast(String message) {
        if (mToast != null) {
            mToast.cancel();
            mToast = null;
        }
        Message msg = mHander.obtainMessage();
        msg.what = SHOWTOAST;
        msg.obj = message;
        mHander.sendMessage(msg);
    }

    /**
     * 显示长通知
     * @param message
     */
    public void showLongToast(String message) {
        if (mToast != null) {
            mToast.cancel();
            mToast = null;
        }
        Message msg = mHander.obtainMessage();
        msg.what = SHOWLONGTOAST;
        msg.obj = message;
        mHander.sendMessage(msg);
    }

    /**
     * 显示带文字的加载对话框
     * @param context
     * @param msg
     */
    public void showLoadingDialog(Context context, String msg) {
        if (loadingDialog == null) {
            loadingDialog = new DetailLoadingDialog(context, R.style.dialog);
            loadingDialog.setCanceledOnTouchOutside(false);//
        }
        loadingDialog.setTitle(msg);
        loadingDialog.show();
    }

    /**
     * 获取子类的类名
     */
    public String getClassName() {
        return this.getClass().getSimpleName();
    }

    /**
     * 显示加载对话框
     * @param context
     * @param titleId
     */
    public void showLoadingDialog(Context context, int titleId) {
        if (loadingDialog == null) {
            loadingDialog = new DetailLoadingDialog(context, R.style.dialog);
            loadingDialog.setCanceledOnTouchOutside(false);//
        }
        loadingDialog.setTitle(titleId);
        loadingDialog.show();
    }

    /**
     * 隐藏对话框
     */
    public void hideLoadingDialog() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    /**
     * 改变加载对话框中文字
     * @param context
     * @param msg
     */
    public void changeLoadingDialog(Context context, String msg) {
        if (loadingDialog == null) {
            loadingDialog = new DetailLoadingDialog(context, R.style.dialog);
            loadingDialog.setCanceledOnTouchOutside(false);//
        }
        loadingDialog.changeTitle(msg);
    }

    /**
     * 改变加载对话框中文字
     * @param context
     * @param titleId
     */
    public void changeLoadingDialog(Context context, int titleId) {
        if (loadingDialog == null) {
            loadingDialog = new DetailLoadingDialog(context, R.style.dialog);
            loadingDialog.setCanceledOnTouchOutside(false);//
        }
        loadingDialog.changeTitle(titleId);
    }

    /**
     * 退出Activity
     */
    public void exit() {
        finish();
        overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
    }

    /**
     * 显示无文字的圆环加载对话框
     */
    public void loadingDialog() {
        if (circleDialog == null) {
            circleDialog = new CustomsizeDialog(this);
            circleDialog.setCanceledOnTouchOutside(false);
        }
        circleDialog.setLoadingText(null);
        circleDialog.setBackGround(null);
        circleDialog.show();
    }

    /**
     * 改变圆环加载对话框中文字
     * @param context
     * @param msg
     */
    public void loadingDialog(Context context, String msg) {
        if (circleDialog == null) {
            circleDialog = new CustomsizeDialog(context);
            circleDialog.setCanceledOnTouchOutside(false);
        }
        circleDialog.setLoadingText(msg);
        circleDialog.setBackGround(null);
        circleDialog.show();
    }

    /**
     * 隐藏对话框
     */
    public void hideloadingDialog() {
        if (circleDialog != null) {
            circleDialog.dismiss();
        }
    }

    /**
     * 判断对话框是否显示
     */
    public boolean isDialogShowing() {
        if (circleDialog != null) {
            return circleDialog.isShowing();
        } else
            return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU) {//MENU键
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    /**
     * LogV方法
     * @param msg
     */
    public void logV(String msg) {
        Log.v(getClassName(), msg);
    }

    /**
     * LogW方法
     * @param msg
     */
    public void logW(String msg) {
        Log.w(getClassName(), msg);
    }

    public void setBaseDialogContext(Context context) {
        this.context = context;
    }

}
